package com.itheima.dao;

import com.itheima.pojo.User;
import com.itheima.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
public interface UserMapper {

    @Select("select * from t_user where username like concat('%', #{username}, '%')")
    List<User> selectByLike(String username);

    /**
     * 分页查询
     * @param startIndex
     * @param pageSize
     * @return
     */
    @Select("select * from t_user limit #{param1} , #{param2}")
    List<User> selectByPage1(Integer startIndex, Integer pageSize);

    /**
     * 分页查询
     * @param startIndex
     * @param pageSize
     * @return
     */
    @Select("select * from t_user limit #{startIndex} , #{pageSize}")
    List<User> selectByPage2(@Param("startIndex") Integer startIndex, @Param("pageSize") Integer pageSize);

    /**
     * 查询所有
     * id = true：是否为主键
     * @Results: 相当于 resultMap标签
     * @Result : 相当于resultMap标签的子标签 result
     * @return
     */
    @Results({
            @Result(id = true,column = "id", property = "id"),
            @Result(column = "user_name", property = "userName" ),
            @Result(column = "home_address", property = "homeAddress" ),
    })
    @Select("select * from t_user")
    List<User> selectAll();
    /**
     * 通过id查询用户基础信息
     */
    @Select("select * from t_user where id = #{id}")
    User selectById(@Param("id") Integer id);

    /**
     * 更新
     * @param user
     * @return
     */
    @Update("UPDATE `t_user` SET  `username`=#{username}, `birthday`= #{birthday}, `sex`=#{sex}, `address`=#{address} WHERE `id`=#{id}")
    int update(User user);
    /**
     * 删除
     * @param id
     * @return
     */
    @Delete("delete from t_user where id = #{id}")
    int deleteById(@Param("id") Integer id);

    /**
     * 插入
     * @SelectKey: 查询主键注解
     *
     *  keyProperty ： 主键属性名
     *  keyColumn :主键列名
     *  resultType: 主键的类型
     *  before:  是否在之前查询( 执行sql的时机 ， 在新增之前查询主键，还是新增后查询主键)
     *  statement: 执行sql语句
     *
     * @param user
     * @return
     */
    @SelectKey(keyProperty = "id" , keyColumn = "id", before = false, resultType = int.class,statement = "select last_insert_id()")
    @Insert("INSERT INTO `t_user` (`id`, `username`, `birthday`, `sex`, `address`) VALUES (null , #{username}, #{birthday}, #{sex}, #{address})")
    int insert(User user);
}
